<template>
	<view class="container">
		<view class="banner">
			<!-- <login :login_show="login_show" @close="login_show=false"></login>
			<template v-if="!accessToken">
				<view class="login-left">
					<image class="avatar" src="@/static/images/myCenter/avatar.png" mode=""></image>
					<view class="login-now" @click="login_show = true">
						立即登录
					</view>
				</view>
			</template> -->
			<template v-if="!accessToken">
				<view class="login-left">
					<image class="avatar" src="@/static/images/myCenter/avatar.png" mode=""></image>
					<view class="login-now">
						立即登录
					</view>
				</view>
			</template>
			<template v-else>
				<view class="avatar">
					<button open-type="chooseAvatar" @chooseavatar="chooseavatar">
						<view class="">
							<image class="avatar" :src="userInfo.headImg" v-if="userInfo.headImg" mode="" ></image>
							<image class="avatar" src="@/static/images/myCenter/avatar.png" v-else mode=""></image>
						</view>
						<view class="edit">
							<u-icon name="camera-fill" size="14" color="#fff"></u-icon>
						</view>
					</button>
				</view>
				<view class="info" :style="{background: globalStyle.sliceBackground}">
					<view class="name">
						<view class="" v-show="!editNameStatus">
							<view class="name_item">
								<view class="title" :style="{color: globalStyle.fontColor}">
									<text>{{userInfo.userName || ''}}</text>
								</view> 
								<view class="edit" @click="editName">
									<u-icon name="edit-pen" size="22" 
										:color="globalStyle.iconColor[0]?globalStyle.iconColor[0]:'#000'"
									></u-icon>
								</view>
							</view>
						</view>
						<view v-show="editNameStatus">
							<view class="name_input_box">
								<input class="name_input" 
									:style="{border: `1rpx solid ${inputBorderColor}`}"
									:focus="inputFocus"
									v-model="userInfoData.cname"
									confirm-type="done"
									type="nickname"
									placeholder-style="color: #e2e2e2;"
									@blur="nameInputBlur"
								/>
							</view>
						</view>
					</view>
					<view class="">
						<view class="text">UID：{{userInfo.id || ''}}</view>
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	import { uploadImages, updateUserInfo } from '@/tools/request/api.js';
	export default {
		name:"userHead",
		data() {
			return {
				login_show: false,
				userInfoData: {
					cname: '',
					avatar_url: ''
				},
				editNameStatus: false,
				inputFocus: false,
			};
		},
		props: {
			inputBorderColor: {
				type: String,
				default: '#fff'
			},
			loginRul: {
				type: String,
				default: ''
			}
		},
		mounted() {
			// this.getUserInfo();
		},
		methods: {
			// 编辑姓名
			editName () {
				this.userInfoData.cname = this.userInfo.userName;
				this.editNameStatus = true;
				setTimeout(() => {
					this.inputFocus = true;
				}, 50)
			},
			// 姓名输入框失去焦点
			nameInputBlur (e) {
				this.inputFocus = false;
				this.editNameStatus = false;
				if (this.userInfoData.cname === this.userInfo.userName) return;
				this.saveInfoFormTap('name');
			},
			// 选择头像
			chooseavatar(img) {
				let self = this;
				uni.showLoading({
					title: '上传头像中',
					mask: true
				});
				let data = {
					file:img.detail.avatarUrl,
					type:1
				}
				uploadImages(data).then(res => {
					if (res.code === 200) {
						uni.hideLoading();
						self.userInfoData.avatar_url = res.body;
						this.saveInfoFormTap('avator');
					} else {
						uni.hideLoading();
						uni.showModal({
							content: res.msg,
							showCancel: false
						});
					}
				})
			},
			// 保存用户信息
			saveInfoFormTap (type) {
				uni.showLoading({
					title: '更新中'
				})
				let postdta = {};
				if (type === 'name') {
					postdta.userName = this.userInfoData.cname;
				} else {
					postdta.headImg = this.userInfoData.avatar_url;
				}
				updateUserInfo(postdta).then(res => {
					if (res.code === 200) {
						uni.hideLoading();
						uni.showToast({
							title: '设置成功',
							mask: true
						});
						this.getUserInfo();
					} else {
						uni.showToast({
							title: '失败！' + res.msg,
							icon: 'none',
							mask: true
						});
					}
				})
			},
			gotoLogin () {
				uni.navigateTo({
					url: this.loginRul
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		
		.banner{
			width: 100%;
			height: 200rpx;
		    display: flex;
			color: black;
			align-items: center;
			box-sizing: border-box;
			padding: 0 30rpx;
			
			.login-left {
				display: flex;
				align-items: center;
				
				.avatar {
					width: 120rpx;
					height: 120rpx;
					margin: 0 20rpx 0 50rpx;
					border-radius: 50%;
				}
				
				.login-now {
					
				}
			}
			
			.avatar{
				position: relative;
				
				image{
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
				}
				
				button{
					padding: 0;
					width: 120rpx;
					height: 120rpx;
					background-color: initial;
				}
				button::after{
				    border:none;
				}
				
				.edit {
					position: absolute;
					width: 40rpx;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 100%;
					bottom: 0;
					right: 0rpx;
					background-color: #2e6cf6;
				}
			}
			
			.info {
				flex: 1;
				min-width: 0;
				color: black;
				padding-left: 30rpx;
			
				.name {
					height: 60rpx;
					padding-bottom: 5rpx;
					margin-bottom: 6rpx;
					font-weight: 500;
					display: flex;
					align-items: center;
					
					.name_item{
						width: 400rpx;
						display: flex;
						align-items: center;
						
						.title {
							color: #000;
							font-size: 38rpx;
							font-weight: bold;
							margin-right: 40rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
					.name_input{
						padding: 0 20rpx;
						border-radius: 20rpx;
						width: 240rpx;
					}
				}
				.text{
					color: #747483;
				}
			}
		}
	}
</style>